<template>
  <div>
    <el-button type="primary" @click="dialogTableVisible = true">添加</el-button>
    <el-dialog v-el-drag-dialog :visible.sync="dialogTableVisible" title="添加" width="600px">
      <el-form ref="row_data" :model="row_data" :rules="rules" label-position="left" label-width="100px" style="width: 450px; margin-left:50px;">
        <el-form-item :label="'登录名'" prop="account_name">
          <el-input v-model="row_data.account_name" placeholder="登录名"/>
        </el-form-item>
        <el-form-item :label="'昵称'" prop="nickname">
          <el-input v-model="row_data.nickname" placeholder="昵称"/>
        </el-form-item>
        <el-form-item :label="'权限角色'" prop="shop_role_id">
          <el-select v-model="row_data.shop_role_id">
            <el-option v-for="op in select_role" :key="op.id" :label="op.name+'（'+op.role+'）'" :value="op.id"/>
          </el-select>
        </el-form-item>
        <el-form-item :label="'密码'" prop="password">
          <el-input v-model="row_data.password" placeholder="密码"/>
        </el-form-item>
        <el-form-item :label="'确认密码'" prop="password">
          <el-input v-model="row_data.password2" placeholder="确认密码"/>
        </el-form-item>
        <el-form-item :label="'头像地址'" prop="head_img">
          <el-alert title="建议图片尺寸为 80 x 80 px" style="margin-bottom: 10px; line-height: 20px" type="success"/>
          <div class="upload_head_img">
            <el-upload :show-file-list="false" :before-upload="beforeAvatarUpload" :http-request="upload_head_img" class="avatar-uploader" action="">
              <img v-if="row_data.head_img" :src="row_data.head_img" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"/>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm()">立即创建</el-button>
          <el-button @click="close()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import elDragDialog from '@/directive/el-dragDialog' // base on element-ui
import { getRoleList, add } from '@/api/user/admin_user'
import store from '@/store'
import { client } from '@/utils/alioss'

export default {
  name: 'DragDialogDemo',
  directives: { elDragDialog },
  data() {
    return {
      select_role: [],
      select_status: [
        { value: 0, label: '禁用' },
        { value: 1, label: '正常' }
      ],
      rules: {
        account_name: [
          { required: true, message: '登录名不能为空', trigger: 'blur' }
        ],
        shop_role_id: [
          { required: true, message: '登录名不能为空', trigger: 'blur' }
        ],
        nickname: [
          { required: true, message: '昵称不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ],
        head_img: [
          { type: 'url', required: false, message: '头像地址地址不正确', trigger: 'blur' }
        ]
      },
      dialogTableVisible: false,
      row_data: {}
    }
  },
  created: function() {
    getRoleList(store.getters.token).then(res => {
      if (res.data.status) {
        this.$set(this, 'select_role', res.data.list.data)
      }
    })
  },
  methods: {
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isPNG = file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 1
      if (!isJPG && !isPNG) {
        this.$message.error('上传图片只能是 JPG、PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 1MB!')
      }
      return (isJPG | isPNG) && isLt2M
    },
    upload_head_img(file) {
      const that = this
      const fileName = 'user-admin_user-head_img' + file.file.uid
      client().put(fileName, file.file).then((res) => {
        this.$set(that.row_data, 'head_img', res.url)
        that.$message.success('头像地址上传成功！')
      }).catch((err) => {
        that.$message.error('头像地址上传失败，原因：' + err)
      })
    },
    submitForm() {
      this.$refs.row_data.validate(valid => {
        if (valid) {
          if (this.row_data.password === this.row_data.password2) {
            const data = {
              'account_name': this.row_data.account_name,
              'nickname': this.row_data.nickname,
              'password': this.row_data.password,
              'shop_role_id': this.row_data.shop_role_id,
              'head_img': this.row_data.head_img
            }
            add(store.getters.token, data).then(res => {
              if (res.data.status) {
                this.$message.success('添加成功！')
                this.$emit('getLists')
                this.close()
              } else {
                this.$message.error('添加失败！原因：' + res.data.msg)
              }
            })
          } else {
            this.$message.error('2次输入的密码不一致！')
          }
        } else {
          return false
        }
      })
    },
    close() {
      this.dialogTableVisible = false
    }
  }
}
</script>

<style>
  /* 图片上传的样式 */
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    color: #8c939d;
    text-align: center;
  }
  .avatar {
    display: block;
  }

  /* 头像地址(head_img)图片上传组件的尺寸 */
  .upload_head_img .avatar-uploader .el-upload{
    border-radius: 40px;
  }
  .upload_head_img .avatar-uploader-icon{
    font-size: 28px;
    width: 80px;
    height: 80px;
    line-height: 80px;
  }
  .upload_head_img .avatar{
    width: 80px;
    height: 80px;
  }
</style>
